package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalIndustryAttrHelper;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * 行业处理类
 * Created by huanghaosheng on 2020/11/12 21:47
 */
@Service
public class IndustryAttrDealHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneCommonService querySceneCommonService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        if (KiteListUtils.isEmpty(commonAttr.getAttrValues())) {
            KiteAttr attr = KiteBeanUtils.transform(commonAttr, KiteAttr.class);
            attr.setModuleId(LocalKeyConsts.GROUPUSERINFO_MODULEID);
            LocalIndustryAttrHelper.initIndustry(attr);
            SpringUtil.getBean(IOperateSceneCommonService.class)
                .setCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), attr);
            List<AttrValue> kiteAttrValues = new ArrayList<>();
            if (KiteListUtils.isNotEmpty(attr.getAttrValues())) {
                kiteAttrValues = attr.getAttrValues().stream().map(kiteIndustry -> {
                    AttrValue attrValue = new AttrValue();
                    attrValue.setAttrValue(kiteIndustry.getAttrValue());
                    attrValue.setAttrValueDesc(kiteIndustry.getAttrValueName());
                    return attrValue;
                }).collect(Collectors.toList());
            }
            commonAttr.setAttrValues(KiteBeanUtils.copyPropsForList(kiteAttrValues, StaticAttrDO.class));
        }
        if (KiteStringUtils.isEmpty(commonAttr.getValue())) {
            LocalAttrHelper.setSpecialAttrValue(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr, this.getClass().getSimpleName());
        }
        LocalAttrHelper.hidden5GCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 行业
        KiteAttr industryAttr = querySceneCommonService.getCommonAttr(sceneInstId, fieldName).getResultObject();
        if (KiteStringUtils.isNotEmpty(value) && value.equals(industryAttr.getValue())) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        //选择行业,联动产品线
        List<KiteAttrValue> productLineAttrList = LocalIndustryAttrHelper.loadProductLine(value);
        // 产品线
        KiteAttr productLineAttr = querySceneCommonService.getCommonAttr(sceneInstId, LocalKeyConsts.PRODUCT_LINE).getResultObject();
        if(Objects.isNull(productLineAttr)) {
        	productLineAttr = querySceneCommonService.getCommonAttr(sceneInstId, LocalKeyConsts.PRODUCT_LINE_NEW).getResultObject();
        }
        if (Objects.nonNull(productLineAttr)) {
            productLineAttr.setValue("");
            productLineAttr.setValueDesc("");
            productLineAttr.setAttrValues(productLineAttrList);
            operateSceneCommonService.setCommonAttr(sceneInstId, productLineAttr);
        }

        // 清空细分重点产品
        KiteAttr keyProductsAttr = querySceneCommonService.getCommonAttr(sceneInstId, LocalKeyConsts.SEGMENT_KEY_PRODUCT).getResultObject();
        if(Objects.isNull(keyProductsAttr)) {
        	keyProductsAttr = querySceneCommonService.getCommonAttr(sceneInstId, LocalKeyConsts.SEGMENT_KEY_PRODUCT_NEW).getResultObject();
        }
        if (Objects.nonNull(keyProductsAttr)) {
            keyProductsAttr.setValue("");
            keyProductsAttr.setValueDesc("");
            keyProductsAttr.setAttrValues(new ArrayList<>());
            operateSceneCommonService.setCommonAttr(sceneInstId, keyProductsAttr);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
